package com.zjjt.dc.modules.srv.service.lw.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwCustomerHoldings;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwPrice;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwPriceDr;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwWh;
import com.zjjt.dc.modules.datacatch.service.IDcCatchDataLwCustomerHoldingsService;
import com.zjjt.dc.modules.datacatch.service.lw.IDcCatchDataLwPriceService;
import com.zjjt.dc.modules.datacatch.service.lw.IDcDataCatchLwWhService;
import com.zjjt.dc.modules.datacatch.vo.lw.DcCatchDataLwPriceVO;
import com.zjjt.dc.modules.datacatch.vo.lw.LwWhVO;
import com.zjjt.dc.modules.datacatch.dto.LwWhDTO;
import com.zjjt.dc.modules.srv.service.lw.ILWPriceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ILWPriceServiceImpl implements ILWPriceService {

    @Resource
    private IDcCatchDataLwPriceService priceService;
    @Resource
    private IDcDataCatchLwWhService whService;
    @Resource
    private IDcCatchDataLwCustomerHoldingsService holdingsService;
    @Override
    public List<DcCatchDataLwPriceVO> getDcCatchDataLwPriceVO(List<DcCatchDataLwPriceDr> list) {
        //获取未处理的数据
        List<DcCatchDataLwWh> unProcess = whService.getUnProcess();
        List<DcCatchDataLwPriceVO> listVO = new ArrayList<>();

        list.forEach(item -> {
            unProcess.stream().filter(f->f.getExUserId().equals(item.getExUserId())&&f.getBreedId().equals(item.getProductCode())).forEach(e->{
                DcCatchDataLwPriceVO dcCatchDataLwPriceVO = JSONObject.parseObject(JSONObject.toJSONString(e), DcCatchDataLwPriceVO.class);
                dcCatchDataLwPriceVO.setDrAmount(item.getAmount());
                //获取价格
                DcCatchDataLwCustomerHoldings one = holdingsService.getOne(new QueryWrapper<DcCatchDataLwCustomerHoldings>()
                        .eq("product_code", e.getBindId())
                        .le("create_time", e.getExPassTime() == null ? e.getExCreateTime() : e.getExPassTime()).last("limit 1"), false);
                if (one!= null){
                    dcCatchDataLwPriceVO.setAmount(one.getAvgInventoryPrice());
                }

                listVO.add(dcCatchDataLwPriceVO);
            });

        });
        return listVO;
    }

    @Override
    public boolean review(List<DcCatchDataLwPriceVO> list) {

        List<DcCatchDataLwWh> cList = new ArrayList<>();
        list.forEach(item -> {
            DcCatchDataLwWh dcCatchDataLwWh = new DcCatchDataLwWh();
            dcCatchDataLwWh.setId(item.getId());
            dcCatchDataLwWh.setPrice(item.getPrice());
            cList.add(dcCatchDataLwWh);
        });
        return  whService.updateBatchById(cList);
    }

    @Override
    public List<LwWhVO> getLwWhVO(LwWhDTO lwWhDTO) {
        return null;
    }
}
